<template>
    <div>
        <div class="nav"></div>
        <div class="wrap">
            <div class="body">
                <div class="title">首次登陆系统，请先修改初始密码</div>
                <div class="line"></div>
                <div class="list">
                    <img class="red" src="../images/main/red.png" alt="">
                    <span class="txt">当前密码：</span>
                    <input type="password" @blur="curPwdBlur" v-model="curPwd" class="input" placeholder="请输入当前密码">
                    <div class="hint" v-if="check.curPwd && !curPwd">请先输入当前密码</div>
                </div>
                <div class="list">
                    <img class="red" src="../images/main/red.png" alt="">
                    <span class="txt">新密码：</span>
                    <input type="password" @blur="newPwdBlur" v-model="newPwd" class="input" placeholder="请输入新密码">
                    <div class="hint" v-if="check.newPwd && !newPwd">请先输入新密码</div>
                </div>
                <div class="list">
                    <img class="red" src="../images/main/red.png" alt="">
                    <span class="txt">确认新密码：</span>
                    <input type="password" @blur="confirmPwdBlur" v-model="confirmPwd" class="input" placeholder="请输入新密码">
                    <div class="hint" v-if="check.confirmPwd && newPwd && confirmPwd && newPwd != confirmPwd">两次密码输入不一致，请重新输入</div>
                    <div class="hint" v-if="check.confirmPwd && !confirmPwd">请先输入新密码</div>
                </div>
                <div class="save" @click="save()">
                    保存
                </div>
            </div>
        </div>
    </div>
</template>


<script>
    import axios from 'axios'
    import qs from 'qs' // 将请求转换成form 表单提交形式

    export default {
        data () {
            return {
              check: {
                curPwd: false,
                newPwd: false,
                confirmPwd: false
              },
              curPwd: '',
              newPwd: '',
              confirmPwd: ''
            }
        },
        methods: {
            newPwdBlur() {
              this.check.newPwd = true;
            },
            curPwdBlur() {
              this.check.curPwd = true;
            },
            confirmPwdBlur() {
              this.check.confirmPwd = true;
            },
            save: function () {
                // this.$router.push({
                //     path:'/main'
                // })
                // this.$cookie.delete ( 'id' );


                 let _this = this;
                if ( !this.curPwd ) {
                    // this.$alert ( '请输入旧密码' );
                    this.check.curPwd = true;
                    return;
                } else if ( !this.newPwd ) {
                    this.check.newPwd = true;
                    // this.$alert ( '请输入新密码' );
                    return;
                } else if ( !this.confirmPwd ) {
                    this.check.confirmPwd = true;
                    // this.$alert ( '请再次输入新密码' );
                    return;
                } else if ( this.newPwd != this.confirmPwd ) {
                    // this.$alert ( '两次输入的密码不一致' );
                    this.check.confirmPwd = true;
                    return;
                }
                
                axios.post (
                    `${this.$store.state.url}/service/updatePwd`,
                    qs.stringify ({
                        id: this.$store.state.userId,
                        newPwd: this.newPwd,
                        oldPwd: this.curPwd
                    })
                ).then ( response => {
                    let res = response.data;
                    if ( res.code == 0 ) {
                        this.$alert ( '修改成功' ).then ( () => {
                            _this.$cookie.set('servicePwd',_this.newPwd)
                            _this.$router.push({
                                path:'/main'
                            })
                        });
                    }
                });
            }
        }
    }
</script>
<style scoped>
.hint {
  color: red;
  margin-left: 150px;
}
.nav {
  background: #353948;
  /* width: 1920px; */
  height: 111px;
}
.wrap {
  width: 100%;
  height: clac(100vh - 211px);
  background: #eceef3;
  padding: 100px 0;
}
.body {
  background: #ffffff;
  box-shadow: 0 5px 20px 0 rgba(0, 0, 0, 0.05);
  border-radius: 6px;
  /* width: 1071px; */
  width: 90%;
  height: 658px;
  margin: 0 auto;
}
.title {
  height: 115px;
  font-family: PingFangSC-Medium;
  font-size: 24px;
  color: #000000;
  letter-spacing: 0;
  line-height: 115px;
  text-align: center;
  font-weight: bold;
}
.line {
  background: #f5f5f5;
  /* width: 861px; */
  height: 3px;
  margin: 0 auto;
}
.list {
  height: 40px;
  /* margin-top: 64px; */
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #000000;
  line-height: 40px;
  width: 500px;
  margin: 60px auto 0;
}

.red {
  width: 10px;
  height: 10px;
  /* margin-left: 285px; */
  /* margin-left: 30% */
}
.txt {
  width: 109px;
  display: inline-block;
  text-align: left;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  font-weight: bold;
  color: #000000;
}
.input {
  background: #ffffff;
  box-shadow: inset 0 1px 3px 0 rgba(0, 0, 0, 0.5);
  width: 307px;
  height: 40px;
  margin-left: 10px;
  padding-left: 10px;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #888888;
  box-sizing: border-box;
}
.save {
  background: #085dab;
  width: 255px;
  height: 55px;
  text-align: center;
  font-family: PingFangSC-Medium;
  font-size: 18px;
  color: #ffffff;
  /* margin-left: 420px; */
  /* margin-top: 102px; */
  cursor: pointer;
  line-height: 55px;
  margin: 80px auto;
}
</style>
<style>
body{
  background-color: #eceef3;
}
</style>
